﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using iShoppingDTO;

namespace iShoppingDAO
{
    public class ThongTinTaiKhoanDAO
    {
        public ThongTinTaiKhoanDTO getThongTinTKOfMa(string maTK)
        {
            ThongTinTaiKhoanDTO tk = null;
            try
            {

                string sql = "SELECT * FROM ThongTinTK WHERE MaTaiKhoan= " + maTK;
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int i, n = dt.Rows.Count;
                for (i = 0; i < n; i++)
                {
                    tk = new ThongTinTaiKhoanDTO();
                    tk.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                    tk.HoTen = dt.Rows[i]["HoTen"].ToString();
                    tk.NgaySinh = dt.Rows[i]["NgaySinh"].ToString();
                    tk.NoiSinh = dt.Rows[i]["NoiSinh"].ToString().Trim();
                    tk.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                    tk.SoDienThoai =dt.Rows[i]["SoDienThoai"].ToString();
                    tk.MaTheTinDung = dt.Rows[i]["MaTheTinhDung"].ToString();
                    tk.Email = dt.Rows[i]["Email"].ToString();
                  
                }

            }
            catch (Exception ex)
            {
                tk = new ThongTinTaiKhoanDTO();
            }
            return tk;
        }

        public static bool insertThongTinTK(ThongTinTaiKhoanDTO tt)
        {
            try
            {
                string sql = string.Format("INSERT INTO ThongTinTK(HoTen, NgaySinh, NoiSinh, DiaChi, SoDienThoai, MaTheTinDung, GioiTinh, Email, MaTaiKhoan) VALUES(N'{0}', '{1}', N'{2}', N'{3}', '{4}', '{5}', N'{6}', '{7}', '{8}')",
                    tt.HoTen, tt.NgaySinh, tt.NoiSinh, tt.DiaChi, tt.SoDienThoai, tt.MaTheTinDung, tt.GioiTinh, tt.Email, tt.MaTaiKhoan);
                bool result = DataProvider.ExecuteNoneQuery(sql);
                return result;
            }
            catch (Exception e)
            {

            }
            return false;
        }

        public static bool checkEmail(string email)
        {
            try
            {
                string sql = string.Format("SELECT * FROM ThongTinTK WHERE Email = '{0}'", email);
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int n = dt.Rows.Count;
                if (n > 0)
                    return true;
            }
            catch (Exception e)
            {

            }
            return false;
        }

        public ThongTinTaiKhoanDTO GetThongTinTkOfMa(string maTK)
        {
            ThongTinTaiKhoanDTO tk = null;
            try
            {

                string sql = "SELECT * FROM ThongTinTK WHERE MaTaiKhoan= '" + maTK + "'";
                DataTable dt = DataProvider.ExecuteQuery(sql);

                int i, n = dt.Rows.Count;
                for (i = 0; i < n; i++)
                {
                    tk = new ThongTinTaiKhoanDTO();
                    tk.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                    tk.HoTen = dt.Rows[i]["HoTen"].ToString();
                    tk.NgaySinh = dt.Rows[i]["NgaySinh"].ToString();
                    tk.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                    tk.SoDienThoai = dt.Rows[i]["SoDienThoai"].ToString();
                    tk.MaTheTinDung = dt.Rows[i]["MaTheTinDung"].ToString();
                    tk.GioiTinh = dt.Rows[i]["GioiTinh"].ToString();
                    tk.Email = dt.Rows[i]["Email"].ToString();
                    tk.MaTaiKhoan = dt.Rows[i]["MaTaiKhoan"].ToString();
                }

            }
            catch (Exception ex)
            {
                tk = new ThongTinTaiKhoanDTO();
            }
            return tk;
        }
        public bool UpdateThongTinTaiKhoan(ThongTinTaiKhoanDTO ttTaiKhoan)
        {
            bool res = false;
            try
            {

                string sql = "UPDATE ThongTinTK SET HoTen= @HoTen, NgaySinh = @NgaySinh, DiaChi = @DiaChi, " +
                   "SoDienThoai = @SoDienThoai, MaTheTinDung = @MaTheTinDung, GioiTinh = @GioiTinh WHERE MaTaiKhoan = @MaTaiKhoan";

                List<SqlParameter> sqlParameters = new List<SqlParameter>();

                SqlParameter[] par = new SqlParameter[7];
                par[0] = new SqlParameter("@HoTen", SqlDbType.NVarChar);
                par[0].Value = ttTaiKhoan.HoTen;
                sqlParameters.Add(par[0]);

                par[1] = new SqlParameter("@NgaySinh", SqlDbType.DateTime);
                par[1].Value = ttTaiKhoan.NgaySinh;
                sqlParameters.Add(par[1]);

                par[2] = new SqlParameter("@DiaChi", SqlDbType.NVarChar);
                par[2].Value = ttTaiKhoan.DiaChi;
                sqlParameters.Add(par[2]);

                par[3] = new SqlParameter("@SoDienThoai", SqlDbType.NVarChar);
                par[3].Value = ttTaiKhoan.SoDienThoai;
                sqlParameters.Add(par[3]);

                par[4] = new SqlParameter("@MaTheTinDung", SqlDbType.NVarChar);
                par[4].Value = ttTaiKhoan.MaTheTinDung;
                sqlParameters.Add(par[4]);

                par[5] = new SqlParameter("@GioiTinh", SqlDbType.NVarChar);
                par[5].Value = ttTaiKhoan.GioiTinh;
                sqlParameters.Add(par[5]);

                par[6] = new SqlParameter("@MaTaiKhoan", SqlDbType.Int);
                par[6].Value = ttTaiKhoan.MaTaiKhoan;
                sqlParameters.Add(par[6]);
                res = DataProvider.ExecuteNoneQuery(sql, sqlParameters);

            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }
    }
}
